/*
	普通订单列表
	订单列表
	订单详情
	订单修改
*/
//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');


//dom 加载完成
$(function(){
	layui.use(['layer', 'form', 'laydate', 'table'], function(){
		var layer = layui.layer,
			form = layui.form,
			laydate = layui.laydate,
			table = layui.table;

		var ajaxData = {
			aId: adminLoginedInfo.aId,
            aToken: adminLoginedInfo.aToken
		};	
		//获取订单列表	
		function getOrders(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			$.ajax({
				url: BASE_SERVICEURL + URlS.order,
				type:'GET',
				data: ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						//console.log(responseData);
						createTable(createTableData(responseData));
						layer.close(loadIndex);
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000});
					location.href = './500.html';
				}
			});
		}
		getOrders(ajaxData)
		//处理表格数据
		function createTableData(responseData){
			var data = responseData.data;
			var table_data_arr = []
			$('#dataSize').text(data.length);
			for(var i = 0; i < data.length; i++){
				var this_data = {
					oId: data[i].o_id,
					consignee: data[i].send_name,
					totalAmount: data[i].order_amount,
					payableAmount: data[i].order_amount,
					orderStatus: data[i].status_name,
					payStatus: data[i].pay_status,
					PaymentMethod: data[i].pay_method,
					paySerialNumber: data[i].payment,
					carrier: data[i].express_com,
					oddNumber: data[i].express_num,
					time: formatTime(data[i].format_time) 
				};
				table_data_arr.push(this_data)
			};
			return table_data_arr;
		}

		//构建表格
		function createTable(tableData){

			table.render({
				elem: '#tableContent',
				height: 315,
				limit: 30, //分页
				page: true,
				cols:[[ //表头
					{field: 'oId', title: '订单编号', sort: true, fixed: 'left'},
					{field: 'consignee', title: '收货人'},
					{field: 'totalAmount', title: '总金额', sort: true},
					{field: 'payableAmount', title: '应付金额', sort: true} ,
					{field: 'orderStatus', title: '订单状态'},
					{field: 'payStatus', title: '支付状态'},
					{field: 'PaymentMethod', title: '支付方式', sort: true},
					{field: 'paySerialNumber', title: '支付流水'},
					{field: 'carrier', title: '物流承运方', sort: true},
					{field: 'oddNumber', title: '物流单号'},
					{field: 'time', title: '下单时间', sort: true},
					{field: 'option', title: '操作', toolbar:'#toolbar'}
				]],
				data:tableData
			});
		};

		// tool 工具条操作
		table.on('tool(order-list-table)', function(obj){
			var data = obj.data;   //本行数据
			var layEvent = obj.event; 
			var tr = obj.tr;  //当前行
			//console.log(data)
			if(layEvent == 'seeDetatil'){  //查看详情
				x_admin_show('查看详情', 'order-detail.html?oId='+data.oId+'', 1200, 600);
			};
		});

	})
})












